﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using HRM.DAL.DAO;

namespace HRM.DAL.Gateway
{
    public class StatusGateway:DBGateway
    {
        
        public bool HasThisStatusExist(Status status)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select Status from tblStatus where Status='"+status.EmpStatus+"'";
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw new Exception("Error occured while reading information in Server");
            }
            finally
            {
                ConnectionObj.Close();
            }
            return false;
        }

        public bool SaveStatus(Status status)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("insert into tblStatus values(@status)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@status", SqlDbType.VarChar).Value = status.EmpStatus;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw new Exception("Error occured during saving Status, Try again !");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public bool EditStatus(int id, string status)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("update tblStatus set Status=@status where Id='"+id+"'");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@status", SqlDbType.VarChar).Value = status;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw new Exception("Error occured during Editing Status, Try again !");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public bool DeleteStatus(object id)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"delete from tblStatus where Id='" + id + "'";
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                
                throw new Exception("Error occured while Deleting");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public List<Status> GetAllStatus()
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select * from tblStatus";
                SqlDataReader reader = CommandObj.ExecuteReader();
                List<Status>statuses=new List<Status>();
                while (reader.Read())
                {
                    Status status=new Status();
                    status.Id = Convert.ToInt32(reader[0]);
                    status.EmpStatus = reader[1].ToString();
                    statuses.Add(status);
                }
                return statuses;
            }
            catch (Exception)
            {
                
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
        }
    }
}